﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PJ.Data
{
    public abstract class ItemWCFProvider<TItemData> : EntityWCFProvider<TItemData>, IItemProvider<TItemData>
        where TItemData : BaseItemData, IItemData, new()
    {
        #region Constructor

        public ItemWCFProvider(IItemProvider provider)
            : base(provider)
        {
        }

        #endregion

        #region Interface implementation

        /// <summary>
        /// Global search of an item based on it's name (and other text fields depending on object type. Like address for partner object).
        /// </summary>
        /// <param name="searchText">Search criteria</param>
        /// <returns>The list of found objects global id or null</returns>
        /// <exception cref="ArgumentNullException"><paramref name="searchText"/> can't be <see langref="null"/> or empty</exception>              
        public IEnumerable<string> Search(string searchText)
        {
            return Provider.Search(searchText);
        }

        /// <summary>
        /// Global search of an item based on it's name (and other text fields depending on object type. Like address for partner object).
        /// Search is made in cache storage first, if not found search is done in data source.
        /// </summary>
        /// <param name="searchText">Search criteria</param>
        /// <returns>The list of found objects global id or null</returns>
        /// <exception cref="ArgumentNullException"><paramref name="searchText"/> can't be <see langref="null"/> or empty</exception>              
        public IEnumerable<string> CachedSearch(string searchText)
        {
                return Provider.CachedSearch(searchText);
        }

        /// <summary>
        /// Gets the <see cref="IItemData">Item data</see> from the data source
        /// </summary>
        /// <param name="name">Entity Name</param>
        /// <returns>The <see cref="IItemData">Item data</see> instance</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> can't be <see langref="null"/> or empty</exception>
        /// <exception cref="DataNotFoundException">If no <see cref="IItemData">Item data</see> exists having Name property equal to <paramref name="name"/></exception>
        public TItemData GetByName(string name)
        {
            return (TItemData)Provider.GetByName(name);
        }

        /// <summary>
        /// Gets the <see cref="IItemData">Item data</see> using cache storage first, if not found in cache gets the <see cref="IItemData">Item data</see> from the provider.
        /// </summary>
        /// <param name="name">Entity Name</param>
        /// <returns>The <see cref="IItemData">Item data</see> instance</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> can't be <see langref="null"/> or empty</exception>
        /// <exception cref="DataNotFoundException">If no <see cref="IItemData">Item data</see> exists having Name property equal to <paramref name="name"/></exception>
        public TItemData CachedGetByName(string name)
        {
                return (TItemData)Provider.CachedGetByName(name);
        }

        /// <summary>
        /// Finds the <see cref="IItemData">Item data</see> from the data source
        /// </summary>
        /// <param name="name">Entity Name</param>
        /// <returns>The data object if found or returns null</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> can't be <see langref="null"/> or empty</exception>
        public TItemData FindByName(string name)
        {
            return (TItemData)Provider.FindByName(name);
        }

        /// <summary>
        /// Finds the <see cref="IItemData">Item data</see> using cache storage first, if not found in cache finds the <see cref="IItemData">Item data</see> from the provider.
        /// </summary>
        /// <param name="name">Entity Name</param>
        /// <returns>The data object if found or returns null</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> can't be <see langref="null"/> or empty</exception>
        public TItemData CachedFindByName(string name)
        {
                return (TItemData)Provider.CachedFindByName(name);
        }

        #endregion

        #region Interface override


        /// <summary>
        /// Gets the <see cref="IItemData">Item data</see> from the data source
        /// </summary>
        /// <param name="name">Entity Name</param>
        /// <returns>The <see cref="IItemData">Item data</see> instance</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> can't be <see langref="null"/> or empty</exception>
        /// <exception cref="DataNotFoundException">If no <see cref="IItemData">Item data</see> exists having Name property equal to <paramref name="name"/></exception>
        IItemData IItemProvider.GetByName(string name)
        {
            return GetByName(name);
        }

        /// <summary>
        /// Gets the <see cref="IItemData">Item data</see> using cache storage first, if not found in cache gets the <see cref="IItemData">Item data</see> from the provider.
        /// </summary>
        /// <param name="name">Entity Name</param>
        /// <returns>The <see cref="IItemData">Item data</see> instance</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> can't be <see langref="null"/> or empty</exception>
        /// <exception cref="DataNotFoundException">If no <see cref="IItemData">Item data</see> exists having Name property equal to <paramref name="name"/></exception>
        IItemData IItemProvider.CachedGetByName(string name)
        {
            return CachedGetByName(name);
        }

        /// <summary>
        /// Finds the <see cref="IItemData">Item data</see> from the data source
        /// </summary>
        /// <param name="name">Entity Name</param>
        /// <returns>The data object if found or returns null</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> can't be <see langref="null"/> or empty</exception>
        IItemData IItemProvider.FindByName(string name)
        {
            return FindByName(name);
        }

        /// <summary>
        /// Finds the <see cref="IItemData">Item data</see> using cache storage first, if not found in cache finds the <see cref="IItemData">Item data</see> from the provider.
        /// </summary>
        /// <param name="name">Entity Name</param>
        /// <returns>The data object if found or returns null</returns>
        /// <exception cref="ArgumentNullException"><paramref name="name"/> can't be <see langref="null"/> or empty</exception>
        IItemData IItemProvider.CachedFindByName(string name)
        {
            return CachedFindByName(name);
        }

        #endregion

        #region Protected

        new protected IItemProvider Provider
        {
            get
            {
                return (IItemProvider)base.Provider;
            }
        }

        #endregion


    }
}
